import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './route'
Vue.use(VueRouter)


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//注册 全局前置守卫函数
router.beforeEach((to,from,next)=>{
  //获取token
  var token = localStorage.getItem('token');
  //判断是否是跳转到 订单 或 我的
  if( to.path == '/index/order' || to.path == '/index/my' ){
    if( token ){ //已登录, 放行
      next();
    }
    else{ //未登录, 跳转到登录
      next('/rejist');
    }
  }else{ //跳转到其他页面
    next();
  }

})
// 
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
